//
// Created by moonlight on 25-11-9.
//
#include"SeqList.h"

void SeqListInit(SeqList* s1) {
    // memset(s1->a,0,sizeof(s1->a)*N);//初始化
    s1->a = NULL;
    s1->capacity = 0;
    s1->size=0;
}

void SeqListPushBack(SeqList* s1, SQDataType x) {
    if(s1->size >= s1->capacity) {
        int newCapacity = s1->capacity == 0 ? 4 : s1->capacity * 2;
        SQDataType* temp = (SQDataType*)realloc(s1->a, s1->capacity * 2 * sizeof(SQDataType));
        if(temp == NULL) {
            printf("Memory allocation failed in SeqListPushBack\n");
            exit(1);
        }
        else {
            s1->a = temp;
            s1->capacity = newCapacity;
        }
    }
    s1->a[s1->size]=x;
    s1->size++;
}
void SeqListPushFont(SeqList* s1, SQDataType x) {
    SeqListCheckCapacity(s1);
    //1.初始条件
    //2.结束条件
    //3.迭代过程
    int end = s1->size-1;
    while (end >= 0) {
        s1->a[end+1] = s1->a[end];
        -- end;
    }
    s1->a[0] = x;
    s1->size++;
}

void PrintList(SeqList* s1) {
    for(int i=0;i<s1->size;i++) {
        printf("%d ",s1->a[i]);
    }
    printf("\n");
}

void SeqListCheckCapacity(SeqList* s1) {
    if(s1->size >= s1->capacity) {
        int newCapacity = s1->capacity == 0 ? 4 : s1->capacity * 2;
        SeqList* temp = (SeqList*)realloc(s1->a, s1->capacity * sizeof(SeqList));
        if(temp == NULL) {
            printf("Memory allocation failed in SeqListPushFont\n");
            exit(1);
        }
        else {
            s1->a = temp;
            s1->capacity = newCapacity;
        }
    }
}